import Vue from 'vue'
import App from './App.vue'
import router from './router/index.js'

// 导入注册应用，和启动应用的方法
import { registerApplication, start } from 'single-spa';

// 手动加载子应用打包出来的文件
const loadScript = async (url) => {
  await new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = url;
    script.onload = resolve;
    script.onerror = reject;
    document.head.appendChild(script)
  });
}
registerApplication(
  'singleVue',
  async () => {
    // 加载模块
    await loadScript('http://localhost:10000/js/chunk-vendors.js'); // vue打包出来的公共模块
    await loadScript('http://localhost:10000/js/app.js'); // vue打包出来的应用核心
    // 生命周期  singleVue上有bootstrap，mount，unmount方法，打包的时候打到singleVue上去的
    return window.singleVue
  },
  location => location.pathname.startsWith('/vue') // 激活标识
)
start();

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

